package servlet;

import java.util.List;

import javabean.Factory;
import javabean.user.User;

import javax.servlet.RequestDispatcher;

public class UserChangePasswordServlet extends BaseServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8538076142868886263L;
	
	public void service() throws Exception{
		String uid = (String) request.getSession().getAttribute("uid");
		if(uid!=null){
			String oldpsw = request.getParameter("oldpsw");
			String psw = request.getParameter("psw");
			int tag = 0;
			try{
				tag = Factory.getUserDAOInstance().changePassword(uid, oldpsw, psw);
			}catch(Exception ex){}
			if(tag==1){
				response.getWriter().write("{\"success\":true,\"msg\":\"修改成功\"}");
			}else if(tag==2){
				response.getWriter().write("{\"success\":false,\"msg\":\"旧密码输入错啦\"}");
			}else{
				response.getWriter().write("{\"success\":false,\"msg\":\"修改失败，换个姿势再试一试\"}");
			}
		}else{
			//跳转
			RequestDispatcher dispatcher = request.getRequestDispatcher("/login");
			dispatcher.forward(request, response) ;
		}
	}
}
